package com.chaofan.authsso.core.web;

import cn.hutool.core.util.StrUtil;
import com.chaofan.authsso.core.common.Constant;
import com.chaofan.authsso.core.common.Methods;
import com.chaofan.authsso.core.common.SsoRestfulMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Author: Chaofan
 * Date: 2020/6/16 18:19
 * email: chaofan2685@qq.com
 */
@RequestMapping(value = "/authsso/service")
public class CheckController extends Methods {

    @Autowired
    HttpSession session;

    /**
     * 校验服务端是否正常
     * @return
     */
    @GetMapping(value = "/check")
    @ResponseBody
    public SsoRestfulMessage<Boolean> checkService(){
        return buildSuccessRest(true);
    }

    /**
     * 校验服务端是否正常
     * @return
     */
    @GetMapping(value = "/loginCheck")
    @ResponseBody
    public SsoRestfulMessage<Boolean> loginCheck(){
        //从session中获取token和回跳地址
        String authToken = (String) session.getAttribute(Constant.SESSION_TOKEN_KEY);
        String redirectUri = (String) session.getAttribute(Constant.SESSION_REDIRECT_URI_KEY);
        if (StrUtil.isEmpty(authToken) || StrUtil.isEmpty(redirectUri)){
            session.setAttribute(Constant.ERROR_MSG_KEY,Constant.SESSION_NO_LOGIN_MSG);
            return buildSuccessRest(false);
        }else {
            return buildSuccessRest(true);
        }
    }

}
